#include "pageharmonic.h"

extern void setView(QComboBox* target);

pageHarmonic::pageHarmonic(QWidget *parent)
            : QWidget(parent)
{
    QFont ft;
    ft.setPointSize(10);

    // paint area
    QVector<double> x3(42), y3(42);

    for (int i=1; i<x3.size(); ++i)
    {
      x3[i] = i;
      y3[i] = 0.05+100*(0.5+qCos(x3[i]*x3[i]*0.2+2)*0.5);
    }

    x3[0] = 0;
    y3[0] = 0;

    QWidget *paintArea = new QWidget;
    paintArea->setFixedSize(950,300);

    harmonicPlot = new QCustomPlot(paintArea);
    harmonicPlot->setFixedSize(950,300);
    harmonicPlot->axisRect()->setupFullAxesBox();

    QLinearGradient gradient(0, 0, 0, 400);
    gradient.setColorAt(0, QColor(90, 90, 90));
    gradient.setColorAt(0.38, QColor(150, 150, 150));
    gradient.setColorAt(1, QColor(70, 70, 70));
    harmonicPlot->setBackground(QBrush(gradient));

    harmonicPlot->xAxis->setLabel("谐波次数");
    harmonicPlot->xAxis->setLabelFont(ft);
    harmonicPlot->yAxis->setLabel("谐波含量(%)");
    harmonicPlot->yAxis->setLabelFont(ft);
    harmonicPlot->xAxis->grid()->setVisible(false);

    QCPBars *bars1 = new QCPBars(harmonicPlot->xAxis, harmonicPlot->yAxis);
    bars1->setWidth(41/(double)x3.size());
    bars1->setData(x3, y3);
    bars1->setPen(QPen(QColor(250, 170, 20).lighter(150)));
    bars1->setBrush(QColor(250, 170, 20));

    harmonicPlot->rescaleAxes();
    harmonicPlot->yAxis->setRange(0, 105);
    harmonicPlot->xAxis->setRange(0, 42);
    harmonicPlot->xAxis->ticker()->setTickCount(20);
    harmonicPlot->yAxis->ticker()->setTickCount(10);
    harmonicPlot->xAxis->setTickLabelFont(ft);
    harmonicPlot->yAxis->setTickLabelFont(ft);
    //harmonicPlot->xAxis->ticker()->setTickStepStrategy(QCPAxisTicker::tssReadability);

    //table area
    harmonicTable = new QTableWidget(2,42,this);
    harmonicTable->verticalHeader()->setFont(ft);

    harmonicTable->setFont(ft);
    harmonicTable->setEditTriggers(QTableView::NoEditTriggers);
    harmonicTable->horizontalHeader()->hide();
    harmonicTable->setFixedSize(950,95);
    harmonicTable->setStyleSheet(".QScrollBar{"
                              "background:#bbb; height: 30px;"
                              "}");

    for(int i=0; i < 42; i++)
    {
        QTableWidgetItem *cubesHeaderItem = new QTableWidgetItem(QString::number(i));
        cubesHeaderItem->setTextAlignment(Qt::AlignCenter);

        harmonicTable->setItem(0,i,cubesHeaderItem);

        QTableWidgetItem *cubesItem = new QTableWidgetItem("0");
        cubesItem->setTextAlignment(Qt::AlignCenter);
        harmonicTable->setItem(1,i,cubesItem );
    }

    QTableWidgetItem *headerItem = new QTableWidgetItem("THD");
    headerItem->setTextAlignment(Qt::AlignCenter);

    harmonicTable->setItem(0,0,headerItem);

    QStringList header;
    header<<"谐波次数"<<"谐波含量";
    harmonicTable->setVerticalHeaderLabels(header);

    //basic tools
    btnBack = new QPushButton("返回");
    btnBack->setFont(ft);
    btnBack->setFixedWidth(100);
    btnBack->hide();

    QLabel *labSelect = new QLabel("谐波成份:");
    labSelect->setFont(ft);

    btnSelectStd = new QRadioButton("标准");
    btnSelectStd->setFont(ft);

    btnSelectDut = new QRadioButton("被测");
    btnSelectDut->setFont(ft);

    btnSelectStd->click();

    cboChannel = new QComboBox(this);
    cboChannel->setFixedWidth(200);
    cboChannel->addItem("Ua");
    cboChannel->addItem("Ub");
    cboChannel->addItem("Uc");
    cboChannel->addItem("Ia");
    cboChannel->addItem("Ib");
    cboChannel->addItem("Ic");
    setView(cboChannel);

    QHBoxLayout *layoutSelect = new QHBoxLayout;
    layoutSelect->addWidget(labSelect);
    layoutSelect->addSpacing(20);
    layoutSelect->addWidget(btnSelectStd);
    layoutSelect->addWidget(btnSelectDut);
    layoutSelect->addStretch(1);
//    layoutSelect->addWidget(btnBack);

    QHBoxLayout *layoutChannel = new QHBoxLayout;
    layoutChannel->addWidget(new QLabel("通道选择:"));
    layoutChannel->addWidget(cboChannel);
    layoutChannel->addStretch(1);
    layoutChannel->addWidget(btnBack);

    harmonicOrder = new QComboBox;
    harmonicOrder->setFixedWidth(100);
    for(int i = 0; i < 40; i++)
    {
        harmonicOrder->addItem(QString::number(i+2));
    }
    setView(harmonicOrder);

    QVBoxLayout *layoutHOrder = new QVBoxLayout;
    layoutHOrder->addWidget(new QLabel("谐波次数"));
    layoutHOrder->addWidget(harmonicOrder);

    dataStd = new QLineEdit("0.00");
    dataDut = new QLineEdit("0.00");
    dataError = new QLineEdit("0.00");

    QVBoxLayout *layoutStd = new QVBoxLayout;
    layoutStd->addWidget(new QLabel("标准(%)"));
    layoutStd->addWidget(dataStd);

    QVBoxLayout *layoutDut = new QVBoxLayout;
    layoutDut->addWidget(new QLabel("被测(%)"));
    layoutDut->addWidget(dataDut);

    QVBoxLayout *layoutwc = new QVBoxLayout;
    layoutwc->addWidget(new QLabel("比差(%)"));
    layoutwc->addWidget(dataError);

    QHBoxLayout *layoutError = new QHBoxLayout;
    layoutError->setContentsMargins(0,5,0,5);
    layoutError->addWidget(new QLabel("谐波误差："));
    layoutError->addLayout(layoutHOrder);
    layoutError->addLayout(layoutStd);
    layoutError->addLayout(layoutDut);
    layoutError->addLayout(layoutwc);

    QWidget *widgetwc = new QWidget;
    widgetwc->setFixedSize(950,70);
    widgetwc->setStyleSheet(".QWidget{background-color:gray;border-radius:5px;}");
    widgetwc->setLayout(layoutError);

    QVBoxLayout *mainLayout = new QVBoxLayout;
    //mainLayout->setContentsMargins(150,20,150,20);
    mainLayout->addLayout(layoutChannel);
    mainLayout->addSpacing(15);
    mainLayout->addWidget(widgetwc);
    mainLayout->addSpacing(15);
    mainLayout->addLayout(layoutSelect);
    mainLayout->addSpacing(15);
    mainLayout->addWidget(harmonicTable);
    mainLayout->addWidget(paintArea);
    mainLayout->addStretch(1);
    setLayout(mainLayout);
}

void pageHarmonic::calError(double std, double dut)
{
    std *= 100.0;

    dataStd->setText(QString::number(std));
    dataDut->setText(QString::number(dut));

    double error = 100.0 * (dut - std)/std;

    dataError->setText(QString::number(error));
}

